<?php
namespace web\admin\controller;

class Article extends Common{
	protected $db;
	public function __construct()
	{
		parent::__construct();
		$this->db = new \system\model\Article();
	}
	public function index(){
		$data = $this->db->getAll(1);
		//p($data);
		View::with('data',$data)->make();
	}
	/**
	 * 添加
	 */
	public function add(){
		if (IS_POST){
			if ($this->db->store()){
				message('添加成功','','success');
			}
			message($this->db->getError(),'','error');
		}
		//获取所属分类的数据
		$cateData = Data::tree(Db::table('category')->get(),'cname');
		//p($cateData);
		//获取标签的数据
		$tagData = Db::table('tag')->get();
		//p($tagData);
		View::with('cateData',$cateData)->with('tagData',$tagData)->make();
	}
	public function edit(){
		$aid = q('get.aid',0,'intval');
		if (IS_POST){
			if ($this->db->edit()){
				message('编辑成功',u('index'),'success');
			}else{
				message($this->db->getError(),'','error');
			}
		}
		//获取旧数据
		$oldData = $this->db->join('article_data','aid','=','article_aid')->where('aid',$aid)->first();
		View::with('oldData',$oldData);
		//获取所有分类的数据
		$cateData = Db::table('category')->get();
		$cateData = Data::tree($cateData,'cname');
//		p($cateData);
		View::with('cateData',$cateData);
		//获取所有标签的数据
		$tagData = Db::table('tag')->get();
		View::with('tagData',$tagData);
		//获取文章添加时的添加标签
		$arcTag = Db::table('article_tag')->where('article_aid',$aid)->lists('tag_tid');
		View::with('arcTag',$arcTag);
		View::make();
	}
	/**
	 * 回收站首页
	 */
	public function recycle(){
		$data = $this->db->getAll(2);
		View::with('data',$data);
		View::make();
	}
	/**
	 * 删除到回收站
	 */
	public function delRecycle(){
		$aid = q('get.aid',0,'intval');
		$this->db->where('aid',$aid)->update(['is_recycle'=>2]);
		message('删除到回收站成功',u('index'),'success');
	}
	/**
	 * 回收站还原
	 */
	public function reduction(){
		$aid = q('get.aid',0,'intval');
		$this->db->where('aid',$aid)->update(['is_recycle'=>1]);
		message('还原成功',u('index'),'success');
	}
	/**
	 * 回收站真正的删除
	 */
	public function delImg(){
		//删除文件
		if (file_exists($_POST['path'])){
			unlink($_POST['path']);
		}
	}
}
